#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
#include "proto.h"

#define	BUFSZ 	(16384)

void	w2f(const char *fname, uint8_t *in, int len)
{
	FILE	*fp;

	fp = fopen(fname, "w");
	if(fp)
	{
		fwrite(in, 1, len, fp);
		fclose(fp);
	}
}

int	main()
{
	func_t f;
	func_t *out, *cur;
	int	num, i, len = 0;
	PARAM *one;
	char	*str;
	FILE	*fp;

	str = malloc(BUFSZ);
	memset(str, 0, BUFSZ);

	proto_load("func.proto", &out, &num);
	fp = fopen("auto.c", "w");

	for(i = 0; i < num; i++)
	{
		cur = out + i;
		memset(str, 0, BUFSZ);
		func_gen(cur, str);
		printf("\ncur func len: %ld\n", strlen(str));
		printf("cur func name: %s\n", cur->name);
		len += strlen(str);
		printf("len : %d\n", len);
		fprintf(fp, "%s\n\n", str);
		fflush(fp);
	}
	fclose(fp);
	return 0;


	memset(&f, 0, sizeof(func_t));
	
	strcpy(f.return_type.name, "CK_RV");
	strcpy(f.name, "C_Encrypt");
	
	f.pa = malloc(sizeof(PARAM) * 5);
	memset(f.pa, 0, sizeof(PARAM) * 5);

	one = f.pa;

	one->desc = PARAM_IN | PARAM_VALUE | PARAM_SINGLE; 
	strcpy(one->type.name, "CK_SESSION_HANDLE");
	strcpy(one->name, "hSession");

	one = f.pa + 1;
	one->desc = PARAM_IN | PARAM_POINT | PARAM_MULTIP; 
	strcpy(one->type.name, "CK_BYTE_PTR");
	strcpy(one->name, "pData");

	one = f.pa + 2;
	one->desc = PARAM_IN | PARAM_VALUE | PARAM_SINGLE;
	strcpy(one->type.name, "CK_ULONG");
	strcpy(one->name, "ulDataLen");

	one = f.pa + 3;
	one->desc = PARAM_OUT | PARAM_POINT | PARAM_MULTIP;
	strcpy(one->type.name, "CK_BYTE_PTR");
	strcpy(one->name, "pEncryptedData");

	one = f.pa + 4;
	one->desc = PARAM_OUT | PARAM_POINT | PARAM_SINGLE;
	strcpy(one->type.name, "CK_ULONG_PTR");
	strcpy(one->name, "pulEncryptedDataLen");

	f.pa_num = 5;
	func_gen(&f, str);

	w2f("gen.c", (uint8_t *)str, strlen(str));
	return 0;
}
